<template>
  <a-card>
    <template #title>
      <a-button type="primary" @click="addList" v-permiss="['add']">添加</a-button>
    </template>
    <a-table row-key="id" :columns="tableColumn" :pagination="pagination" :data-source="tableData">
      <template #operation="{record}">
        <a-button type="link" @click="editList(record)" v-permiss="['update']">修改</a-button>
      </template>
    </a-table>

    <a-modal v-model:visible="visible.isShow" :title="visible.title" @ok="confirm"
             :afterClose="clearForm"
             width="70%"
             :maskClosable="false">
      <div style="margin-bottom: 10px">
        <a-month-picker style="width:220px" v-model:value="form.month" valueFormat="YYYY-MM" placeholder="请选择月份"
                        format="YYYY-MM"/>
        <div style="margin-top: 15px;display:flex">
          <a-upload
              accept=".csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
              name="file"
              :showUploadList="false"
              :before-upload="excelFileUpload"
          >
            <a-button type="primary" style="margin-right: 15px">
              <upload-outlined></upload-outlined>
              导入
            </a-button>
          </a-upload>
          <a-button><a href="https://ddhkapps.oss-cn-hongkong.aliyuncs.com/doc/profit_loss_template.xlsx">下载模板</a>
          </a-button>
        </div>
      </div>
      <a-table row-key="value" :columns="addTableColumn" bordered :pagination="false" :data-source="addTableData">
        <template #title="{record}">
          <div v-html="record ?  record.title : ''"></div>
        </template>
        <template #value="{record,index}">
          <a-input style="width:100%" @change="changeValueComputed(index,record,$event)"
                   v-model:value="form[record.value]"/>
        </template>
      </a-table>
    </a-modal>
  </a-card>
</template>

<script>
import {ref, onMounted, reactive, computed, toRefs, h} from 'vue'
import {importProfitLoss, increaseProfitLoss, fetchProfitLossList, updateProfitLoss} from '../../../api/finance'
import {message} from "ant-design-vue";
import XLSX from "xlsx"
import {UploadOutlined} from "@ant-design/icons-vue";

const tableColumn = [
  {title: '日期', dataIndex: 'month', width: 150, align: 'center'},
  {title: '更新时间', dataIndex: 'updateTime', width: 150, align: 'center'},
  {title: '操作', width: 100, align: 'center', slots: {customRender: 'operation',}},
]

const addTableData = [
  {oneTitle: '一、主营业务收入', twoTitle: '利息收入', threeTitle: '-', value: 'businessIncomeInterestIncome'},
  {oneTitle: '一、主营业务收入', twoTitle: '罚息收入', threeTitle: '-', value: 'businessIncomePenaltyInterestIncome'},
  {oneTitle: '一、主营业务收入', twoTitle: '手续费收入', threeTitle: '-', value: 'businessIncomeCommissionIncome'},
  {oneTitle: '一、主营业务收入', twoTitle: '展期手续费收入', threeTitle: '-', value: 'businessIncomeIncomeFromExtensionFees'},
  {oneTitle: '一、主营业务收入', twoTitle: '历史逾期收入', threeTitle: '-', value: 'businessIncomeHistoricalOverdueIncome'},
  {oneTitle: '一、主营业务收入', twoTitle: '业务额外收入', threeTitle: '-', value: 'businessAdditionalIncome'},
  {oneTitle: '一、主营业务收入', twoTitle: '合计', threeTitle: '-', value: 'mainBusinessIncomeSubtotal'},
  {oneTitle: '二、其他业务收入', twoTitle: '其他业务收入', threeTitle: '-', value: 'otherBusinessIncome'},
  {oneTitle: '二、其他业务收入', twoTitle: '合计', threeTitle: '-', value: 'otherBusinessIncomeSubtotal'},

  {oneTitle: '三、主营业务成本', twoTitle: '支付公司费用', threeTitle: '放款手续费', value: 'businessCostServiceChargeForLoan'},
  {
    oneTitle: '三、主营业务成本',
    twoTitle: '支付公司费用',
    threeTitle: '回款手续费',
    value: 'businessCostServiceChargeForPaymentCollection'
  },
  {oneTitle: '三、主营业务成本', twoTitle: '销账费用', threeTitle: '-', value: 'businessCostWriteOffExpenses'},
  {oneTitle: '三、主营业务成本', twoTitle: '事业部服务费', threeTitle: '-', value: 'businessCostServiceDivisionFee'},
  {oneTitle: '三、主营业务成本', twoTitle: '投放费用', threeTitle: '-', value: 'businessCostAgentInvestment'},
  {oneTitle: '三、主营业务成本', twoTitle: '短信费用', threeTitle: '通知/验证码短信', value: 'businessCostVerificationCodeSms'},
  {oneTitle: '三、主营业务成本', twoTitle: '短信费用', threeTitle: '营销短信', value: 'businessCostMarketingSms'},
  {oneTitle: '三、主营业务成本', twoTitle: '电话费用', threeTitle: '语音验证', value: 'businessCostMarketingVoice'},
  {oneTitle: '三、主营业务成本', twoTitle: '电话费用', threeTitle: '语音电话', value: 'businessCostVoicePhone'},
  {oneTitle: '三、主营业务成本', twoTitle: '邮件费用', threeTitle: '营销邮件', value: 'businessCostMarketingEmail'},
  {oneTitle: '三、主营业务成本', twoTitle: 'AI费用', threeTitle: '智能催收', value: 'businessCostIntelligentCollection'},
  {oneTitle: '三、主营业务成本', twoTitle: '用户注册费', threeTitle: '-', value: 'businessCostUserRegistrationFee'},
  {oneTitle: '三、主营业务成本', twoTitle: '风控费用', threeTitle: '黑名单', value: 'businessCostBlacklist'},
  {oneTitle: '三、主营业务成本', twoTitle: '风控费用', threeTitle: '黑猫风控', value: 'businessCostScore'},
  {oneTitle: '三、主营业务成本', twoTitle: '风控费用', threeTitle: '三方风控', value: 'businessCostScoreThirdParty'},
  {oneTitle: '三、主营业务成本', twoTitle: '风控费用', threeTitle: '银行卡校验费', value: 'businessCostBankCardVerificationFee'},
  {oneTitle: '三、主营业务成本', twoTitle: '风控费用', threeTitle: '运营商费用', value: 'businessCostOperatorFees'},
  {oneTitle: '三、主营业务成本', twoTitle: '风控费用', threeTitle: '身份校验费', value: 'businessCostIdentityVerificationFee'},
  {oneTitle: '三、主营业务成本', twoTitle: '风控费用', threeTitle: 'OCR+活体检测+人脸比对', value: 'ocrDetectionComparison'},
  {oneTitle: '三、主营业务成本', twoTitle: 'APP费用', threeTitle: 'APK包', value: 'businessCostApkPackage'},
  {oneTitle: '三、主营业务成本', twoTitle: 'APP费用', threeTitle: 'APP上架包', value: 'businessCostAppOnShelfPackage'},
  {oneTitle: '三、主营业务成本', twoTitle: 'APP费用', threeTitle: 'APP维护费', value: 'businessCostAppMaintenanceFee'},
  {oneTitle: '三、主营业务成本', twoTitle: '外包费用', threeTitle: '座席费', value: 'businessCostSeatFee'},
  {oneTitle: '三、主营业务成本', twoTitle: '外包费用', threeTitle: '佣金', value: 'businessCostCommission'},
  {oneTitle: '三、主营业务成本', twoTitle: 'SAAS服务费用', threeTitle: '打款服务', value: 'businessCostPaymentService'},
  {oneTitle: '三、主营业务成本', twoTitle: '数据费用', threeTitle: '-', value: 'businessCostDataService'},
  {oneTitle: '三、主营业务成本', twoTitle: '税金及附加', threeTitle: '-', value: 'businessCostTaxesAndSurcharges'},
  {oneTitle: '三、主营业务成本', twoTitle: '合计', threeTitle: '-', value: 'mainBusinessCostSubtota'},

  {oneTitle: '四、营业费用', twoTitle: '员工工资', threeTitle: '-', value: 'operatingExpensesEmployeeSalary'},
  {oneTitle: '四、营业费用', twoTitle: '员工奖金', threeTitle: '-', value: 'operatingExpensesEmployeeBonus'},
  {oneTitle: '四、营业费用', twoTitle: '社保费', threeTitle: '-', value: 'operatingExpensesSocialSecurityFee'},
  {oneTitle: '四、营业费用', twoTitle: '房租费', threeTitle: '-', value: 'operatingExpensesRent'},
  {oneTitle: '四、营业费用', twoTitle: '水电费', threeTitle: '-', value: 'operatingExpensesWaterAndElectricity'},
  {oneTitle: '四、营业费用', twoTitle: '物业管理费', threeTitle: '-', value: 'operatingExpensesPropertyManagementFee'},
  {oneTitle: '四、营业费用', twoTitle: '办公费', threeTitle: '-', value: 'operatingExpenseOfficeExpensess'},
  {oneTitle: '四、营业费用', twoTitle: '交通费', threeTitle: '-', value: 'operatingExpensesTrafficExpense'},
  {oneTitle: '四、营业费用', twoTitle: '福利费', threeTitle: '-', value: 'operatingExpensesWelfareFunds'},
  {oneTitle: '四、营业费用', twoTitle: '业务招待费', threeTitle: '-', value: 'operatingExpensesBusinessEntertainmentExpenses'},
  {oneTitle: '四、营业费用', twoTitle: '通讯费', threeTitle: '-', value: 'operatingExpensesCommunicationFee'},
  {oneTitle: '四、营业费用', twoTitle: '私卡代持费', threeTitle: '-', value: 'operatingExpensesPrivateCardHoldingFee'},
  {oneTitle: '四、营业费用', twoTitle: '法人代持费', threeTitle: '-', value: 'operatingExpensesLegalPersonHoldingFee'},
  {oneTitle: '四、营业费用', twoTitle: '项目服务费', threeTitle: '-', value: 'operatingExpensesProjectServiceFee'},
  {oneTitle: '四、营业费用', twoTitle: '差旅费', threeTitle: '-', value: 'operatingExpensesTravelExpenses'},
  {oneTitle: '四、营业费用', twoTitle: '地址挂靠费', threeTitle: '-', value: 'operatingExpensesAddressAffiliationFee'},
  {oneTitle: '四、营业费用', twoTitle: '服务费', threeTitle: '-', value: 'operatingExpensesServiceCharge'},
  {oneTitle: '四、营业费用', twoTitle: '合计', threeTitle: '-', value: 'operatingExpensesSubtota'},


  {oneTitle: '五、管理费用', twoTitle: '管理工资', threeTitle: '-', value: 'managementExpensesManagementSalary'},
  {oneTitle: '五、管理费用', twoTitle: '管理绩效', threeTitle: '-', value: 'managementExpensesManagementPerformance'},
  {oneTitle: '五、管理费用', twoTitle: '管理奖金', threeTitle: '-', value: 'managementExpensesManagementBonus'},
  {oneTitle: '五、管理费用', twoTitle: '社保费', threeTitle: '-', value: 'managementExpensesSocialSecurityFee'},
  {oneTitle: '五、管理费用', twoTitle: '住房公积金', threeTitle: '-', value: 'managementExpensesAccumulationFund'},
  {oneTitle: '五、管理费用', twoTitle: '房租费', threeTitle: '-', value: 'managementExpensesRent'},
  {oneTitle: '五、管理费用', twoTitle: '水电费', threeTitle: '-', value: 'managementExpensesWaterAndElectricity'},
  {oneTitle: '五、管理费用', twoTitle: '物业管理费', threeTitle: '-', value: 'managementExpensesPropertyManagementFee'},
  {oneTitle: '五、管理费用', twoTitle: '办公费', threeTitle: '-', value: 'managementExpensesOfficeExpenses'},
  {oneTitle: '五、管理费用', twoTitle: '交通费', threeTitle: '-', value: 'managementExpensesTrafficExpense'},
  {oneTitle: '五、管理费用', twoTitle: '福利费', threeTitle: '-', value: 'managementExpensesWelfareFunds'},
  {oneTitle: '五、管理费用', twoTitle: '业务招待费', threeTitle: '-', value: 'managementExpensesBusinessEntertainmentExpenses'},
  {oneTitle: '五、管理费用', twoTitle: '通讯费', threeTitle: '-', value: 'managementExpensesCommunicationFee'},
  {oneTitle: '五、管理费用', twoTitle: '电脑租赁费', threeTitle: '-', value: 'managementExpensesRentalFee'},
  {oneTitle: '五、管理费用', twoTitle: '咨询费', threeTitle: '-', value: 'managementExpensesConsultationFee'},
  {oneTitle: '五、管理费用', twoTitle: '差旅费', threeTitle: '-', value: 'managementExpensesDepreciationFee'},
  {oneTitle: '五、管理费用', twoTitle: '法人代持费', threeTitle: '-', value: 'managementExpensesLegalPersonHoldingFee'},
  {oneTitle: '五、管理费用', twoTitle: '工位费', threeTitle: '-', value: 'managementExpensesStationFee'},
  {oneTitle: '五、管理费用', twoTitle: '服务费', threeTitle: '-', value: 'managementExpensesServiceFee'},
  {oneTitle: '五、管理费用', twoTitle: '合计', threeTitle: '-', value: 'managementExpensesSubtota'},

  {oneTitle: '六、财务费用', twoTitle: '手续费', threeTitle: '-', value: 'financialExpensesServiceCharge'},
  {oneTitle: '六、财务费用', twoTitle: '汇兑损益', threeTitle: '-', value: 'financialExpensesExchangeGainsAndLosses'},
  {oneTitle: '六、财务费用', twoTitle: '利息支出', threeTitle: '-', value: 'financialExpensesInterestExpense'},
  {oneTitle: '六、财务费用', twoTitle: '银行服务费', threeTitle: '-', value: 'financialExpensesBankServiceCharge'},
  {oneTitle: '六、财务费用', twoTitle: '合计', threeTitle: '-', value: 'financialExpensesSubtota'},

  {oneTitle: '七、资产减值损失', twoTitle: 'M1(1-7)', threeTitle: '-', value: 'assetsImpairmentLossM1'},
  {oneTitle: '七、资产减值损失', twoTitle: 'M2(8-15)', threeTitle: '-', value: 'assetsImpairmentLossM2'},
  {oneTitle: '七、资产减值损失', twoTitle: 'M3(16-30)', threeTitle: '-', value: 'assetsImpairmentLossM3'},
  {oneTitle: '七、资产减值损失', twoTitle: 'M3+(30天+)', threeTitle: '-', value: 'assetsImpairmentLossM3a'},
  {oneTitle: '七、资产减值损失', twoTitle: '展期本金', threeTitle: '-', value: 'assetsImpairmentLossRenewalPrincipal'},
  {oneTitle: '七、资产减值损失', twoTitle: '合计', threeTitle: '-', value: 'assetsImpairmentLossSubtota'},

  {oneTitle: '八、营业外收入', twoTitle: '营业外收入', threeTitle: '-', value: 'nonOperatingIncomeOtherIncome'},
  {oneTitle: '八、营业外收入', twoTitle: '合计', threeTitle: '-', value: 'nonOperatingIncomeSubtota'},

  {oneTitle: '九、营业外支出', twoTitle: '营业外支出', threeTitle: '-', value: 'nonOperatingExpenses'},
  {oneTitle: '九、营业外支出', twoTitle: '合计', threeTitle: '-', value: 'nonOperatingExpensesSubtota'},
  {oneTitle: '净利润 [ = 1+2-3-4-5-6-7+8-9 ]', value: 'netProfitSubtota'},
]
console.log(addTableData)

const addTableColumn = [
  {
    title: '一级科目名称', dataIndex: 'oneTitle', align: 'center', customRender: ({text, index}) => {
      const renderObj = {
        children: text,
        props: {},
      }
      if (text === '一、主营业务收入') {
        renderObj.props.rowSpan = index === 0 ? 7 : 0
      } else if (text === '二、其他业务收入') {
        renderObj.props.rowSpan = index === 7 ? 2 : 0
      } else if (text === '三、主营业务成本') {
        renderObj.props.rowSpan = index === 9 ? 28 : 0
      } else if (text === '四、营业费用') {
        renderObj.props.rowSpan = index === 37 ? 18 : 0
      } else if (text === '五、管理费用') {
        renderObj.props.rowSpan = index === 55 ? 20 : 0
      } else if (text === '六、财务费用') {
        renderObj.props.rowSpan = index === 75 ? 5 : 0
      } else if (text === '七、资产减值损失') {
        renderObj.props.rowSpan = index === 80 ? 6 : 0
      } else if (text === '八、营业外收入') {
        renderObj.props.rowSpan = index === 86 ? 2 : 0
      } else if (text === '九、营业外支出') {
        renderObj.props.rowSpan = index === 88 ? 2 : 0
      } else if (index === 90) {
        renderObj.props.colSpan = 3
      }
      return renderObj
    }
  },
  {
    title: '二级科目名称', dataIndex: 'twoTitle', align: 'center', customRender: ({text, index}) => {
      const renderObj = {
        children: text,
        props: {},
      }
      if (text === '支付公司费用') {
        renderObj.props.rowSpan = index === 9 ? 2 : 0
      } else if (text === '短信费用') {
        renderObj.props.rowSpan = index === 14 ? 2 : 0
      } else if (text === '电话费用') {
        renderObj.props.rowSpan = index === 16 ? 2 : 0
      } else if (text === '风控费用') {
        renderObj.props.rowSpan = index === 21 ? 7 : 0
      } else if (text === 'APP费用') {
        renderObj.props.rowSpan = index === 28 ? 3 : 0
      } else if (text === '外包费用') {
        renderObj.props.rowSpan = index === 31 ? 2 : 0
      } else if (index === 90) {
        renderObj.props.colSpan = 0
      }

      return renderObj
    }
  },
  {
    title: '三级科目名称', dataIndex: 'threeTitle', align: 'center', customRender: ({text, index}) => {
      const renderObj = {
        children: text,
        props: {},
      }
      if (index === 90) {
        renderObj.props.colSpan = 0
      }
      return renderObj
    }
  },
  {title: '参数', dataIndex: 'value', align: 'center', slots: {customRender: 'value',}},
]
export default {
  name: "LossDetailsManage",
  components: {
    UploadOutlined
  },
  setup() {
    const state = reactive({
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      tableData: [],
      visible: {
        isShow: false,
        title: '',
        type: ''
      },
      form: {
        month: null
      },

    })
    const pagination = computed(() => ({
      current: state.page.pageNum,
      total: state.page.total,
      pageSize: state.page.pageSize,
      showSizeChanger: true,
      showQuickJumper: true,
      pageSizeOptions: ['10', '50', '100', '200'],
      showTotal: total => total + '条',
      onChange: (page) => {
        state.page.pageNum = page
        getTableData()
      },
      onShowSizeChange: (page, size) => {
        state.page.pageNum = page
        state.page.pageSize = size
        getTableData()
      }
    }))

    async function getTableData() {
      const businessId = sessionStorage.getItem('businessValue')
      if (!businessId) return message.error('请选择事业部')
      const res = await fetchProfitLossList({
        ...state.page,
        businessId: parseInt(businessId)
      })
      if (res.code === 200) {
        const {list, total} = res.data
        state.tableData = list
        state.page.total = total
      }
    }


    function addList() {
      state.visible = {
        isShow: true,
        type: 'add',
        title: '添加'
      }
    }

    function editList(record) {
      state.form = {...record}
      state.visible = {
        isShow: true,
        type: 'update',
        title: '修改'
      }
    }

    function clearForm() {
      state.form = {
        month: null
      }
    }

    function excelFileUpload(file) {
      let reader = new FileReader()
      reader.onload = async (e) => {
        let bstr = e.target.result;
        let wb = XLSX.read(bstr, {type: 'binary', cellDates: true});
        let wsname = wb.SheetNames[0];
        let ws = wb.Sheets[wsname];
        let data = XLSX.utils.sheet_to_json(ws, {header: 1});
        if (data.length > 0) {
          try {
            const dataValue = data.filter((item, index) => index !== 0)
            const keys = {}
            dataValue.forEach((item, index) => {
              if (item.length >= 4) {
                keys[addTableData[index].value] = item[3]
              }
            })
            state.form = {
              ...state.form,
              ...keys,
            }
            message.success('导入成功')
          } catch (e) {
            message.error('导入失败 ' + e)
          }
        }

      }
      reader.readAsBinaryString(file);
    }

    // 设置输入框值
    function changeValueComputed(index, record, value) {
      if (index < 6) {
        setMainBusinessIncomeSubtotal()
      }
      if (index === 7) {
        setOtherBusinessIncomeSubtotal()
      }
      if (index >= 9 && index <= 35) {
        setMainBusinessCostSubtotal()
      }
      if (index >= 37 && index <= 53) {
        setOperatingExpensesSubtotal()
      }
      if (index >= 55 && index <= 73) {
        setManagementExpensesSubtotal()
      }
      if (index >= 75 && index <= 78) {
        setFinancialExpensesSubtotal()
      }
      if (index >= 80 && index <= 84) {
        setAssetsImpairmentLossSubtotal()
      }
      if (index === 86) {
        setNonOperatingIncomeOtherIncome()
      }
      if (index === 88) {
        setNonOperatingExpenses()
      }
      //  设置净利润
      setNetProfitSubtotal()
    }

    // 主营业务收入合计
    function setMainBusinessIncomeSubtotal() {
      const d1 = parseFloat(state.form[addTableData[0].value] || 0)
      const d2 = parseFloat(state.form[addTableData[1].value] || 0)
      const d3 = parseFloat(state.form[addTableData[2].value] || 0)
      const d4 = parseFloat(state.form[addTableData[3].value] || 0)
      const d5 = parseFloat(state.form[addTableData[4].value] || 0)
      const d6 = parseFloat(state.form[addTableData[5].value] || 0)
      const value = (d1 + d2 + d3 + d4 + d5 + d6).toFixed(2)
      state.form['mainBusinessIncomeSubtotal'] = value
    }

    // 其他业务收入合计
    function setOtherBusinessIncomeSubtotal() {
      const d1 = parseFloat(state.form[addTableData[7].value] || 0)
      const value = d1.toFixed(2)
      state.form['otherBusinessIncomeSubtotal'] = value
    }

    // 主营业务成本合计
    function setMainBusinessCostSubtotal() {
      let arrValue = []
      addTableData.forEach((item, index) => {
        if (index >= 9 && index <= 35) {
          const value = state.form[addTableData[index].value]
          if (value) {
            arrValue.push(parseFloat(value || 0))
          }
        }
      })
      state.form['mainBusinessCostSubtota'] = arrValue.reduce(((previousValue, currentValue) => {
        return previousValue + currentValue
      })).toFixed(2)
    }

    // 营业费用合计
    function setOperatingExpensesSubtotal() {
      let arrValue = []
      addTableData.forEach((item, index) => {
        if (index >= 37 && index <= 53) {
          const value = state.form[addTableData[index].value]
          if (value) {
            arrValue.push(parseFloat(value || 0))
          }
        }
      })
      state.form['operatingExpensesSubtota'] = arrValue.reduce(((previousValue, currentValue) => {
        return previousValue + currentValue
      })).toFixed(2)
    }

    // 管理费用合计
    function setManagementExpensesSubtotal() {
      let arrValue = []
      addTableData.forEach((item, index) => {
        if (index >= 55 && index <= 73) {
          const value = state.form[addTableData[index].value]
          if (value) {
            arrValue.push(parseFloat(value || 0))
          }
        }
      })
      state.form['managementExpensesSubtota'] = arrValue.reduce(((previousValue, currentValue) => {
        return previousValue + currentValue
      })).toFixed(2)
    }

    // 财务费用合计
    function setFinancialExpensesSubtotal() {
      let arrValue = []
      addTableData.forEach((item, index) => {
        if (index >= 75 && index <= 78) {
          const value = state.form[addTableData[index].value]
          if (value) {
            arrValue.push(parseFloat(value || 0))
          }
        }
      })
      state.form['financialExpensesSubtota'] = arrValue.reduce(((previousValue, currentValue) => {
        return previousValue + currentValue
      })).toFixed(2)
    }

    // 资产减值损失合计
    function setAssetsImpairmentLossSubtotal() {
      let arrValue = []
      addTableData.forEach((item, index) => {
        if (index >= 80 && index <= 84) {
          const value = state.form[addTableData[index].value]
          if (value) {
            arrValue.push(parseFloat(value || 0))
          }
        }
      })
      state.form['assetsImpairmentLossSubtota'] = arrValue.reduce(((previousValue, currentValue) => {
        return previousValue + currentValue
      })).toFixed(2)
    }

    // 营业外收入合计
    function setNonOperatingIncomeOtherIncome() {
      const d1 = parseFloat(state.form[addTableData[86].value] || 0)
      const value = d1.toFixed(2)
      state.form['nonOperatingIncomeSubtota'] = value
    }

    // 营业外支出合计
    function setNonOperatingExpenses() {
      const d1 = parseFloat(state.form[addTableData[88].value] || 0)
      const value = d1.toFixed(2)
      state.form['nonOperatingExpensesSubtota'] = value
    }

    // 设置净利润
    function setNetProfitSubtotal() {
      const d8 = parseFloat(state.form[addTableData[6].value] || 0)
      const d10 = parseFloat(state.form[addTableData[8].value] || 0)
      const d38 = parseFloat(state.form[addTableData[36].value] || 0)
      const d56 = parseFloat(state.form[addTableData[54].value] || 0)
      const d76 = parseFloat(state.form[addTableData[74].value] || 0)
      const d81 = parseFloat(state.form[addTableData[79].value] || 0)
      const d87 = parseFloat(state.form[addTableData[85].value] || 0)
      const d89 = parseFloat(state.form[addTableData[87].value] || 0)
      const d91 = parseFloat(state.form[addTableData[89].value] || 0)
      const value = (d8 + d10 - d38 - d56 - d76 - d81 - d87 + d89 - d91).toFixed(2)
      state.form['netProfitSubtota'] = value
    }

    async function confirm() {
      if (!state.form.month) {
        return message.error('请选择月份')
      }
      const isAdd = state.visible.type === 'add'
      const api = (params) => isAdd ? increaseProfitLoss(params) : updateProfitLoss(params)
      const res = await api({
        ...state.form,
        businessId: parseInt(sessionStorage.getItem('businessValue'))
      })
      if (res.code === 200) {
        message.success(isAdd ? '添加成功' : '修改成功')
        getTableData()
        state.visible.isShow = false
      }
    }

    onMounted(() => {
      getTableData()
    })
    return {
      changeValueComputed,
      excelFileUpload,
      addTableData,
      addTableColumn,
      pagination,
      tableColumn,
      clearForm,
      confirm,
      addList,
      editList,
      ...toRefs(state)
    }
  }
}
</script>

<style scoped>

</style>
